@layout("/common/_container.html"){
<div class="layui-card">
	<div class="layui-form layui-card-header layuiadmin-card-header-auto">
	  <div class="layui-form-item">
	    <div class="layui-inline">
	      	<label class="layui-form-label">发票类型</label>
	      	<div class="layui-input-block">
				<select name="billType"	id="billType">
					<option value="">发票类型</option>
					<option value="0">增值税普通发票</option>
					<option value="1">增值税专用发票</option>
				</select>		  
			</div>
	    </div>
	    	<div class="layui-inline">
	    	<label class="layui-form-label">审核状态</label>
	    	<div class="layui-input-block">
	    		<select name="billState" id="billState">
	    			<option value="">审核状态</option>
	    			<option value="0">未开具</option>
	    			<option value="1">已开具</option>
	    			<option value="3">已驳回</option>
	    		</select>
	    	</div>
	     </div>
	    <div class="layui-inline">
	    	<label class="layui-form-label">订单号</label>
	    	<div class="layui-input-block">
	    		<input type="text" name="orderId" id="orderId" autocomplete="off" placeholder="订单号" class="layui-input" />
	    	</div>
	    </div>
	    </div>
	    <div class="layui-form-item">
	    <div class="layui-inline">
	    	<label class="layui-form-label">申请时间</label>
	    	<div class="layui-input-inline">
	    		<input type="text" name="startTime" id="startTime" autocomplete="off" placeholder="开始日期" class="layui-input"/>
	    	</div>
	    	<div class="layui-form-mid">-</div>
	    	<div class="layui-input-inline">
	    		<input type="text" name="endTime" id="endTime" autocomplete="off" placeholder="结束日期" class="layui-input"/>
	    	</div>
	    
	    </div>
	    <div class="layui-inline">
			<label class="layui-form-label"></label>
			<label class="layui-form-label"></label>
			<button class="layui-btn layuiadmin-btn-useradmin" data-type="reload">
				<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
			</button>
	    </div>
			<div class="layui-inline"  style="float:right;">
				<label class="layui-form-label"></label>
				@if(shiro.hasPermission("/bill/export")){
					<button class="layui-btn layui-btn-normal layui-btn-radius" id="export" lay-filter="export" lay-submit>
						&nbsp;导&nbsp;&nbsp;出&nbsp;
					</button>
				@}
			</div>
	   </div>
	  </div>
  <div class="layui-card-body">
  @if(shiro.hasPermission("/bill/add")){
  <div style="padding-bottom: 10px;">
      <button class="layui-btn layuiadmin-btn-useradmin" data-type="add"><i class="layui-icon layui-icon-add-1"></i>添加</button>
  </div>
  @}
  <table class="layui-hidden" id="table" lay-filter="table"></table>
  	<script type="text/html" id="oper">
		  	{{# if(d.billState==0){}}
            @if(shiro.hasPermission("/bill/review")){
             <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="review" title="审核发票">审核发票</a>
             @}
           {{# } else if(d.billState==1){}}
           @if(shiro.hasPermission("/bill/detail")){
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail" title="发票详情">发票详情</a>
            @}
           {{# }else if(d.billState==2){}}
             @if(shiro.hasPermission("/bill/sent")){
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="sent" title="发票寄送">发票寄送</a>
             @}
			{{#  }}}
	</script>
  </div>
</div>

<script>
layui.config({
	base: Feng.ctxPath + "/static/layuiadmin/" //静态资源所在路径
}).extend({
	index: 'lib/index' //主入口模块
}).use(['index', 'layer','table', 'form','laydate'],function(){
	
	var $ = layui.$
  	,form = layui.form
  	,table = layui.table
	,laydate=layui.laydate;
	var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 });
	table.render({
	    elem: '#table'
	    ,url: Feng.ctxPath + '/bill/list'
	    ,cellMinWidth: 100
	    ,height: 'full-168' //设置高度
	    ,cols: [[
	        {title: '发票抬头', field: 'billTitle'},
	        {title: '发票类型', field: 'billType',width:130},
	        {title: '开具类型', field: 'opererType'},
	        {title: '发票金额', field: 'orderPrice'},
	        {title: '审核状态', field: 'billStatas'},
	        {title: '申请时间', field: 'createTime'},
	        {title: '订单号',  field:'orderId'},
	        {title: '操作',fixed: 'right', width:140, align:'center',fixed: 'right', toolbar: '#oper'}
	    ]]
	    ,page:true
	    ,loading:true
	    ,text: {
	        none: '暂无相关数据' 
	      }
	    ,done: function(res){
	    	layer.close(loadingIndex);
        }
	});
	
	//开始日期
    var insStart = laydate.render({
      elem: '#startTime'
     ,max:0
      /* ,min: 0 */
      ,done: function(value, date){
        //更新结束日期的最小日期
        insEnd.config.min = lay.extend({}, date, {
      	month: date.month - 1
        });
        
        //自动弹出结束日期的选择器
       insEnd.config.elem[0].focus();
      }
    });
    
    //结束日期
    var insEnd = laydate.render({
      elem: '#endTime'
      ,max:0
     /*  ,min: 0 */
      ,done: function(value, date){
        //更新开始日期的最大日期
        insStart.config.max = lay.extend({}, date, {
          month: date.month - 1
        });
      }
    });
	
	//事件
	var active = {
		//加载
	   	reload: function(){
	       var billType = $('#billType').val();
	       var billState = $('#billState').val();
	       var orderId= $('#orderId').val();
	       var startTime= $('#startTime').val();
	       var endTime= $('#endTime').val();
	       var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 });
	       //执行表格重载
	       table.reload('table', {
	    	 height: 'full-168' //设置高度
	    	 ,where: {
	    		  billType:billType,
	  	          billState:billState,
	  	          orderId:orderId,
	  	          startTime:startTime,
	  	          endTime:endTime
	         }
	       ,page:true
	       ,done: function(res){
		    	layer.close(loadingIndex);
	        }
	      });
	    },
	    reset: function(){
	    	$("#billType").val("");
	    	$("#billState").val("");
	    	$("#orderId").val("");
	    	$("#startTime").val("");
	    	$("#endTime").val("");
	    	form.render();	
	    },
   };
  
   
   $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
     var type = $(this).data('type');
     active[type] ? active[type].call(this) : '';
   });

	form.on('submit(export)', function (data) {
		var field = data.field; //获取提交的字段
		//墨绿深蓝风
		layer.alert('正在导出. . .完成后请手动关闭弹窗', {
			skin: 'layui-layer-lan'
			,closeBtn: 0
			//,anim: 4 //动画类型
		});
		// alert(JSON.stringify(field))
		//模拟form表单提交导出
		postExcelFile(field, Feng.ctxPath + "/bill/export");
	});

 //监听工具条
   table.on('tool(table)', function(obj){
     var data = obj.data;
     if(obj.event === 'review'){
         var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 });
         layer.open({
           type: 2
           ,title: '审核发票处理'
           ,content: Feng.ctxPath + '/bill/bill_review/'+data.billId
           ,area: ['720px','415px']
           ,btn: ['确定', '取消']
           ,yes: function(index, layero){
             var iframeWindow = window['layui-layer-iframe'+ index]
             ,submitID = 'bill-submit'
             ,submit = layero.find('iframe').contents().find('#'+ submitID);
             //监听提交
             iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                 var loading2 = layer.load(2);
               var field = data.field; //获取提交的字段
               var ajax = new $ax(Feng.ctxPath + "/bill/review", function (data) {
                   Feng.success("处理成功!");
                   table.reload('table',{
                 	  done: function(res){
            		    	layer.close(loadingIndex);
            	          }
                    }); //数据刷新
                   layer.close(loading2); //关闭弹层
                   layer.close(index); //关闭弹层
               }, function (data) {
                   Feng.error("处理失败!" + data.responseJSON.message + "!");
               },true);
               ajax.setData(field);
               ajax.start();
               
             });  
             
             submit.trigger('click');
           }
           ,btn2: function(index, layero){
      	   		layer.close(loadingIndex);
           }
           ,cancel:function(index, layero){
      	        layer.close(loadingIndex);
           }
         });
       }else if(obj.event === 'sent'){
       var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 });
       layer.open({
         type: 2
         ,title: '发票寄送'
         ,content: Feng.ctxPath + '/bill/bill_sent/'+data.addressId
         ,area: ['720px', '415px']
         ,btn: ['确定', '取消']
         ,yes: function(index, layero){
           var iframeWindow = window['layui-layer-iframe'+ index]
           ,submitID = 'bill_sent-submit'
           ,submit = layero.find('iframe').contents().find('#'+ submitID);
           //监听提交
           iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
               var loading2 = layer.load(2);
             var field = data.field; //获取提交的字段
             var ajax = new $ax(Feng.ctxPath +"/bill/sent", function (data) {
                 Feng.success("处理成功!");
                 table.reload('table',{
               	  done: function(res){
          		    	layer.close(loadingIndex);
          	          }
                  }); //数据刷新
                 layer.close(index); //关闭弹层
                 layer.close(loading2); //关闭弹层
             }, function (data) {
                 Feng.error("处理失败!" + data.responseJSON.message + "!");
             },true);
             ajax.setData(field);
             ajax.start();
             
           });  
           
           submit.trigger('click');
         }
         ,btn2: function(index, layero){
    	   layer.close(loadingIndex);
         }
         ,cancel:function(index, layero){
    	   layer.close(loadingIndex);
         }
       });
     }if(obj.event === 'detail'){
         var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 });
         layer.open({
           type: 2
           ,title: '审核发票处理'
           ,content: Feng.ctxPath + '/bill/detail/'+data.billId+"/"+data.addressId
           ,area: ['720px','415px']
           ,btn: ['取消']
           ,btn1: function(index, layero){
        	   layer.close(index);
      	   	   layer.close(loadingIndex);
           }
           ,cancel:function(index, layero){
      	        layer.close(loadingIndex);
           }
         });
       }
   });


	//模拟form表单提交导出
	function postExcelFile(params, url) { //params是post请求需要的参数，url是请求url地址
		var form = document.createElement("form");
		form.style.display = 'none';
		form.action = url;
		form.method = "post";
		document.body.appendChild(form);

		for(var key in params){
			var input = document.createElement("input");
			input.type = "hidden";
			input.name = key;
			input.value = params[key];
			form.appendChild(input);
		}

		form.submit();
		form.remove();
	}
});
</script>
@}
